<template>
  <div px-43px v-if="res">
    <div v-if="res.isShowTitle == 1" text-60px mt-60px break-all font-800 line-height-86px>
      {{ res.title }}
    </div>
    <div v-if="res.isShowPublishTime == 1" text-35px flex items-center mt-29px font-400 mb-29px>
      <span v-if="!!res.source" mr-60px>{{ res.source }}</span>
      <span>{{ res.publishTime }}</span>
    </div>
    <div class="rich-text" mt-60px pb-30 v-html="richText"></div>
  </div>
  <back></back>
</template>

<script setup lang="ts">
import { getArticleDetail } from '@/api/index'
import { onLoad } from '@dcloudio/uni-app'
import { startRedirectToHome, stopRedirectToHome } from '@/utils/screenSaver'
const res = ref<any>(null)
// const res = ref<any>('') // 文章内容
const way = ref<any>({
  USER_AGREEMENT: {
    title: '服务协议',
    type: 'USER_AGREEMENT'
  },
  PRIVACY_POLICY: {
    title: '隐私政策',
    type: 'PRIVACY_POLICY'
  },
  LICENSE_INFORMATION: {
    title: '证照信息',
    type: 'LICENSE_INFORMATION'
  },
  ABOUT: {
    title: '关于我们',
    type: 'ABOUT'
  },
  STORE_REGISTER: {
    title: '店铺入驻协议',
    type: 'STORE_REGISTER'
  },
  DISTRIBUTION_AGREEMENT: {
    title: '分销推广协议',
    type: 'DISTRIBUTION_AGREEMENT'
  }
})

const richText = computed(() => {
  return res.value.content
    .replace(/<img /g, '<img style="width:100%;height:auto;display:block;" ')
    .replace(/<video /g, '<video style="width:100%;height:auto;display:block;" ')
})

onLoad(async (option: any) => {
  if (option.contentId) {
    const result = await getArticleDetail(option.contentId)
    res.value = result.data.data
  }
  // else {
  //   uni.setNavigationBarTitle({
  //     title: way.value[option.type].title,
  //   });
  //   const result = await getArticleDetailByType(way.value[option.type].type)
  //   if (result.data.success) {
  //     res.value = result.data.result.content
  //   }
  // }
})

const resetTimer = () => {
  console.log('用户操作，重置跳转计时')
  startRedirectToHome() // 重置跳转逻辑
}
onMounted(() => {
  startRedirectToHome()
  // 监听用户操作事件
  document.addEventListener('click', resetTimer)
  document.addEventListener('touchstart', resetTimer)
})
onUnmounted(() => {
  stopRedirectToHome()
  // 移除用户操作事件监听
  document.removeEventListener('click', resetTimer)
  document.removeEventListener('touchstart', resetTimer)
})
// onMounted(() => {
//   const updateFontSize = () => {
//     const screenWidth = uni.getSystemInfoSync().screenWidth
//     const remBase = (screenWidth / 375) * 16 // 1rem 动态值
//     document.documentElement.style.fontSize = `${remBase}px`
//     console.log('设备基础fontSize: ' + remBase + 'px')
//   }

//   // 初始化时设置字体大小
//   updateFontSize()

//   // 监听窗口大小变化
//   window.addEventListener('resize', updateFontSize)

//   // 清理监听器（组件卸载时）
//   onUnmounted(() => {
//     window.removeEventListener('resize', updateFontSize)
//   })
// })
</script>

<style scoped>
.rich-text {
  word-wrap: break-word; /* 允许在单词内断行 */
  overflow-wrap: break-word; /* 允许在单词内断行 */
  word-break: break-all; /* 强制在单词内断行 */
}
</style>
